﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Approval;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Approval
{
    public class UpdApprovalCommandHandler : IRequestHandler<UpdApprovalCommand, APIResult<object>>
    {
        private readonly IBaseRepository<LeaveApplication> leaveappRep;
        private readonly IMapper mapper;

        public UpdApprovalCommandHandler(IBaseRepository<LeaveApplication> leaveappRep, IMapper mapper)
        {
            this.leaveappRep = leaveappRep;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(UpdApprovalCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var entity =leaveappRep.GetAll().Result.FirstOrDefault(x => x.LeaveId == request.LeaveId);
            entity.ApplicantName = request.ApplicantName;
            entity.DepartMentid = request.DepartMentid;
            entity.LeaveType = request.LeaveType;
            entity.StartTime = request.StartTime.AddHours(8);
            entity.EndTime = request.EndTime.AddHours(8);
            entity.TimeSpan = request.TimeSpan;
            entity.ReasonLeave = request.ReasonLeave;
            entity.RegistrationTime = request.RegistrationTime;
            entity.DepartmentHead = request.DepartmentHead;
            entity.UpdateName = request.NickName;
            entity.UpdateTime = DateTime.Now;
            var r = leaveappRep.Update(entity).Result;
            if (r > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = r;
            }

            return await Task.FromResult(result);
        }
    }
}
